-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
46 lines (36 loc) · 1.31 KB
/
Solution.java
File metadata and controls
46 lines (36 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.*;
public class DFSGraph {
private Map<Integer, List<Integer>> graph = new HashMap<>();
private Set<Integer> visited = new HashSet<>();
public void addEdge(int u, int v) {
graph.computeIfAbsent(u, k -> new ArrayList<>()).add(v);
graph.computeIfAbsent(v, k -> new ArrayList<>()).add(u); // For undirected graph
}
public void DFS(int v) {
System.out.print(v + " ");
visited.add(v);
for (int u : graph.getOrDefault(v, new ArrayList<>())) {
if (!visited.contains(u)) {
DFS(u);
}
}
}
public static void main(String[] args) {
DFSGraph dfsGraph = new DFSGraph();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of nodes: ");
int n = scanner.nextInt();
System.out.print("Enter the number of edges: ");
int edges = scanner.nextInt();
System.out.println("Enter the edges (u v):");
for (int i = 0; i < edges; i++) {
int u = scanner.nextInt();
int v = scanner.nextInt();
dfsGraph.addEdge(u, v);
}
System.out.print("Enter the starting node: ");
int start = scanner.nextInt();
System.out.println("DFS Traversal: ");
dfsGraph.DFS(start);
}
}